package cz.muni.fi.pa165.web;

import cz.muni.restinterface.CourierDTO;
import cz.muni.service.CourierServiceLocal;
import java.util.List;
import javax.ejb.EJB;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontValidate;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.HandlesEvent;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;

/**
 *
 * @author OSiRiS-PC
 */

@UrlBinding("/CourierManager.{$event}")
public class ManagerCourierActionBean implements ActionBean {
    
    private ActionBeanContext context;
    
    @ValidateNestedProperties({
        @Validate(field="firstName", required=true, maxlength=75),
        @Validate(field="lastName", required=true,maxlength=75),
        @Validate(field="userName", required=true,maxlength=75)
    })
    private CourierDTO courier;

    public CourierDTO getCourier() {
        return courier;
    }

    public void setCourier(CourierDTO courier) {
        this.courier = courier;
    }
    
    @EJB(mappedName="java:global/DeliveryCompany-web/CourierService!cz.muni.service.CourierServiceLocal")
    private CourierServiceLocal courierService;

    @Override
    public void setContext(ActionBeanContext abc) {
        this.context = abc;
    }

    @Override
    public ActionBeanContext getContext() {
       return context;
    }
    
    @DefaultHandler
    public Resolution allcouriers() {
        return new ForwardResolution("/manager_couriers.jsp");
    }
    
    
    @HandlesEvent("add")
      public Resolution add() {
        courierService.create(courier);
        return new RedirectResolution("/manager_couriers.jsp");
    }
    
    @DontValidate
    @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "update"})
    public void loadCourierFromDatabase() {
        String ids = context.getRequest().getParameter("courier.id");
        if (ids == null) return;
        courier = courierService.find(Long.parseLong(ids));
    }
 
    @DontValidate
    public Resolution edit() {
        return new ForwardResolution("/editCourier.jsp");
    }
 
    @DontValidate
    public Resolution update() {
        courierService.update(courier);
        return new RedirectResolution("/manager_couriers.jsp");
    }
    
    @DontValidate
    public Resolution delete() {
        try {
            courierService.delete(courier);
        } catch (Exception e) {
            return new RedirectResolution("/manager_error.jsp");
        }
        courierService.delete(courier);
        return new RedirectResolution("/manager_couriers.jsp");
    }
    
    /**
     * 
     * @return 
     */
    public List<CourierDTO> getCouriers() {
        
        return courierService.findAll();
    
    }
    
}
